package com.macro.mall.controller;

import com.macro.mall.common.api.CommonResult;
import com.macro.mall.model.StoreCarousel;
import com.macro.mall.service.StoreCarouselService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 门店轮播图管理Controller
 * Created on 2025-09-16
 */
@RestController
@Api(tags = "StoreCarouselController")
@Tag(name = "StoreCarouselController", description = "门店轮播图管理")
@RequestMapping("/carousel")
@Slf4j
public class StoreCarouselController {
    
    @Autowired
    private StoreCarouselService storeCarouselService;
    
    /**
     * 获取轮播图列表
     */
    @ApiOperation("获取轮播图列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<List<StoreCarousel>> list() {
        try {
            List<StoreCarousel> carouselList = storeCarouselService.list();
            log.info("获取轮播图列表成功，数量: {}", carouselList.size());
            return CommonResult.success(carouselList);
        } catch (Exception e) {
            log.error("获取轮播图列表失败", e);
            return CommonResult.failed("获取轮播图列表失败: " + e.getMessage());
        }
    }
    
    /**
     * 根据ID获取轮播图详情
     */
    @ApiOperation("根据ID获取轮播图详情")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public CommonResult<StoreCarousel> getById(
            @ApiParam("轮播图ID") @PathVariable Long id) {
        try {
            StoreCarousel carousel = storeCarouselService.getById(id);
            if (carousel != null) {
                log.info("获取轮播图详情成功: id={}", id);
                return CommonResult.success(carousel);
            } else {
                log.warn("轮播图不存在: id={}", id);
                return CommonResult.failed("轮播图不存在");
            }
        } catch (Exception e) {
            log.error("获取轮播图详情失败: id={}", id, e);
            return CommonResult.failed("获取轮播图详情失败: " + e.getMessage());
        }
    }
    
    /**
     * 添加轮播图
     */
    @ApiOperation("添加轮播图")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public CommonResult<Object> add(@RequestBody Map<String, String> request) {
        try {
            String imageUrl = request.get("imageUrl");
            if (imageUrl == null || imageUrl.trim().isEmpty()) {
                log.warn("添加轮播图失败: 图片URL为空");
                return CommonResult.validateFailed("图片URL不能为空");
            }
            
            int result = storeCarouselService.create(imageUrl.trim());
            if (result > 0) {
                log.info("添加轮播图成功: imageUrl={}", imageUrl);
                return CommonResult.success(null, "添加轮播图成功");
            } else {
                log.error("添加轮播图失败: imageUrl={}", imageUrl);
                return CommonResult.failed("添加轮播图失败");
            }
        } catch (IllegalArgumentException e) {
            log.warn("添加轮播图参数错误: {}", e.getMessage());
            return CommonResult.validateFailed(e.getMessage());
        } catch (IllegalStateException e) {
            log.warn("添加轮播图状态错误: {}", e.getMessage());
            return CommonResult.failed(e.getMessage());
        } catch (Exception e) {
            log.error("添加轮播图失败", e);
            return CommonResult.failed("添加轮播图失败: " + e.getMessage());
        }
    }
    
    /**
     * 删除轮播图
     */
    @ApiOperation("删除轮播图")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public CommonResult<Object> delete(
            @ApiParam("轮播图ID") @PathVariable Long id) {
        try {
            int result = storeCarouselService.delete(id);
            if (result > 0) {
                log.info("删除轮播图成功: id={}", id);
                return CommonResult.success(null, "删除轮播图成功");
            } else {
                log.error("删除轮播图失败: id={}", id);
                return CommonResult.failed("删除轮播图失败");
            }
        } catch (IllegalArgumentException e) {
            log.warn("删除轮播图参数错误: {}", e.getMessage());
            return CommonResult.validateFailed(e.getMessage());
        } catch (Exception e) {
            log.error("删除轮播图失败: id={}", id, e);
            return CommonResult.failed("删除轮播图失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取轮播图统计信息
     */
    @ApiOperation("获取轮播图统计信息")
    @RequestMapping(value = "/stats", method = RequestMethod.GET)
    public CommonResult<Map<String, Object>> getStats() {
        try {
            int totalCount = storeCarouselService.getTotalCount();
            boolean canAdd = storeCarouselService.canAddCarousel();
            
            Map<String, Object> stats = new HashMap<>();
            stats.put("totalCount", totalCount);
            stats.put("maxCount", 5);
            stats.put("canAdd", canAdd);
            stats.put("remainingCount", canAdd ? (5 - totalCount) : 0);
            
            log.info("获取轮播图统计信息成功: totalCount={}, canAdd={}", totalCount, canAdd);
            return CommonResult.success(stats);
        } catch (Exception e) {
            log.error("获取轮播图统计信息失败", e);
            return CommonResult.failed("获取轮播图统计信息失败: " + e.getMessage());
        }
    }
}
